<template>
  <div class="iframe-panel-wrapper">
    <div class="iframe-panel-list">
      <el-card
        class="panel-card"
        shadow="hover"
        :body-style="{ padding: '0', height: '100%' }"
      >
        <template #header>
          <div class="panel-header">
            <span class="header-title">操作面板列表</span>
          </div>
        </template>
        <el-list class="list-container" :data="panelItems" :border="false">
          <el-list-item
            v-for="item in panelItems"
            :key="item.id"
            class="list-item"
          >
            <el-button
              class="item-button"
              type="text"
              @click="handleItemClick(item)"
              :class="{ 'item-button-active': activeItem === item.id }"
            >
              <div class="button-content">
                <div class="title-section">
                  <h4 class="item-title">{{ item.title }}</h4>
                </div>
              </div>
            </el-button>
          </el-list-item>
        </el-list>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { setAlgorithmParam } from "@/api/util";
import { ElMessage } from "element-plus";

// 导入需要的组件
import { defineAsyncComponent } from "vue";
import sendToUnity from "@/core/communication/SendToUnity";
import { SendMessageType } from "@/core/config/DeclareGlobal";
import { resolve } from "@kangc/v-md-editor";

// 异步导入所需组件
const AdjustLoadComponent = defineAsyncComponent(() =>
  import("@/components/pop_up/AdjustLoadComponent.vue")
);
const WaterFlowAdjustmentComponent = defineAsyncComponent(() =>
  import("@/components/pop_up/WaterFlowAdjustmentComponent.vue")
);
const AirwayCleanComponent = defineAsyncComponent(() =>
  import("@/components/pop_up/AirwayCleanComponent.vue")
);
const GeneratorParameter = defineAsyncComponent(() =>
  import("@/components/pop_up/GeneratorParameter.vue")
);
const TrendChart = defineAsyncComponent(() =>
  import("@/components/pop_up/TrendChart.vue")
);
const AlarmEvents = defineAsyncComponent(() =>
  import("@/components/pop_up/AlarmEvents.vue")
);
const PTCCheckComponent = defineAsyncComponent(() =>
  import("@/components/pop_up/PTCCheckComponent.vue")
);

// 定义响应式数据 - 用于组件切换
const currentComponent = ref(null);

// 定义组件的props
const props = defineProps({
  // 当前激活的面板项ID
  activeItemId: {
    type: String,
    default: null,
  },
});

// 定义组件的事件
const emit = defineEmits(["item-click", "component-loaded"]);

// 定义响应式数据
const activeItem = ref(null);

// 定义面板列表数据
const panelItems = ref([
  {
    id: "load",
    title: "调节负载",
    description: "调整发电机负载参数",
    callback: function () {
      return new Promise((resolve) => {
        currentComponent.value = AdjustLoadComponent;
        resolve();
      });
    },
  },
  {
    id: "water",
    title: "调节水流量",
    description: "控制发电机冷却水流",
    callback: function () {
      return new Promise((resolve) => {
        currentComponent.value = WaterFlowAdjustmentComponent;
        resolve();
      });
    },
  },
  {
    //查看风路情况
    id: "checkAirway",
    title: "查看风路",
    description: "",
    callback: () => {
      return new Promise((resolve) => {
        sendToUnity.send({
          type: SendMessageType.ViewWindRoad,
          data: {},
        });
      });
    },
  },
  {
    id: "airway",
    title: "清理风道",
    description: "维护发电机通风系统",
    callback: function () {
      return new Promise((resolve) => {
        // 延迟1秒加载AirwayCleanComponent

        currentComponent.value = AirwayCleanComponent;
        resolve();
      });
    },
  },
  {
    id: "ptc",
    title: "查看PTC接线",
    description: "检查PTC系统接线状态",
    callback: function () {
      return new Promise((resolve) => {
        currentComponent.value = PTCCheckComponent;
        resolve();
      });
    },
  },
  {
    id: "alarm",
    title: "查看机组控制屏",
    description: "检查机组控制屏状态",
    callback: function () {
      return new Promise((resolve) => {
        sendToUnity.send({
          type: SendMessageType.ViewTerminalBlock,
          data: {},
        });
        setTimeout(() => {
          currentComponent.value = GeneratorParameter;
          resolve();
        }, 1000);
      });
    },
  },
  {
    id: "alarm",
    title: "查看曲线趋势",
    description: "查看机组运行趋势曲线",
    callback: function () {
      return new Promise((resolve) => {
        currentComponent.value = TrendChart;
        resolve();
      });
    },
  },
  {
    id: "alarm",
    title: "查看报警信息",
    description: "检查机组报警状态",
    callback: function () {
      return new Promise((resolve) => {
        currentComponent.value = AlarmEvents;
        resolve();
      });
    },
  },
  {
    //查看爆炸图
    id: "checkExplose",
    title: "查看爆炸图",
    description: "查看爆炸图",
    callback: () => {
      return new Promise((resolve) => {
        sendToUnity.send({
          type: SendMessageType.GeneratorExplosion,
          data: {},
        });
        resolve();
      });
    },
  },
]);

// 处理列表项点击事件
const handleItemClick = (item) => {
  console.log("Panel item clicked:", item);
  activeItem.value = item.id;

  // 调用item中的callback函数
  if (item.callback && typeof item.callback === "function") {
    try {
      item.callback().then(() => {
        // 发送事件给父组件，包含当前激活的组件
        emit("component-loaded", {
          item,
          component: currentComponent.value,
          title: item.title,
        });
      });
    } catch (error) {
      console.error("Error executing callback:", error);
    }
  }

  // 保持向后兼容，仍然发出原来的事件
  emit("item-click", item);
};

// 为panelItems设置唯一的id
panelItems.value = panelItems.value.map((item, index) => {
  if (item.id === "alarm") {
    return { ...item, id: `${item.id}-${index + 1}` };
  }
  return item;
});

// 暴露方法供父组件调用
defineExpose({
  panelItems,
  handleItemClick,
  activeItem,
});
</script>

<style scoped>
.iframe-panel-wrapper {
  width: 100%;
}

.iframe-panel-list {
  width: 100%;
  min-height: 50vh;
  background-color: #22474f;
  border-radius: 12px;
  /* padding: 16px; */
}

.panel-card {
  height: 100%;
  min-height: 50vh;
  background-color: #22474f;
  overflow: hidden;
  transition: all 0.3s ease;
  border: none;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 12px 12px 0 0;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  color: white;
}

.item-count {
  background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(2px);
  font-weight: bold;
}

.list-container {
  background-color: #22474f;
  height: calc(100% - 60px); /* 减去头部高度 */
  overflow-y: auto;
  color: white;
}

/* 自定义滚动条样式 */
.list-container::-webkit-scrollbar {
  width: 6px;
}

.list-container::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.list-container::-webkit-scrollbar-thumb {
  background: #c0c4cc;
  border-radius: 3px;
}

.list-container::-webkit-scrollbar-thumb:hover {
  background: #909399;
}

.list-item {
  padding: 0;
  margin: 0;
  color: white;
}

.item-button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  margin: 0;
  border-bottom: 1px solid #f0f2f5;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  color: white;
}

.item-button:hover {
  background-color: #2c5a63;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  color: white;
}

.item-button-active {
  background-color: #3a7b88 !important;
  color: white !important;
  font-weight: 500;
}

.button-content {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  flex: 1;
}

.title-section {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 0;
}

.item-title {
  font-size: 16px;
  color: white;
  font-weight: 600;
  margin: 0;
}

.item-badge {
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
}

.badge-load,
.badge-water,
.badge-airway {
  background-color: #e6f7ff;
  color: #1890ff;
}

.badge-ptc {
  background-color: #fff1f0;
  color: #ff4d4f;
}

/* 添加动画效果 */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.list-item {
  animation: fadeInUp 0.5s ease-out;
}

.list-item:nth-child(1) {
  animation-delay: 0.1s;
}
.list-item:nth-child(2) {
  animation-delay: 0.2s;
}
.list-item:nth-child(3) {
  animation-delay: 0.3s;
}
.list-item:nth-child(4) {
  animation-delay: 0.4s;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .iframe-panel-list {
    padding: 8px;
  }

  .panel-header {
    padding: 12px 16px;
  }

  .header-title {
    font-size: 16px;
  }

  .item-button {
    padding: 12px 16px;
  }

  .item-title {
    font-size: 14px;
  }
}
</style>